{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "ICS 104 - Introduction to Programming in Python and C\n",
    "## Introduction (Chapter 1: Sections 1-6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Learning Outcomes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Learn about computers and programming\n",
    "- Write and run your first Python program\n",
    "- Recognize compile-time and run-time errors"
   ]
  },
  {
   "attachments": {
    "apple-technology-ipad-computer-38568s05.png": {
     "image/png": "[encoded data, 119732 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Computer Programs\n",
    "- The **computer** is a machine that **stores** data (numbers, words, pictures), **interacts** with devices (the monitor, the sound system, the printer, the scanner), and **executes** programs.\n",
    "![apple-technology-ipad-computer-38568s05.png](attachment:apple-technology-ipad-computer-38568s05.png)"
   ]
  },
  {
   "attachments": {
    "scanner.jpg": {
     "image/jpeg": "[encoded data, 22044 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- A computer **program** tells a computer, in minute detail, the sequence of steps that are needed to fulfill a task.\n",
    "    - *The act of designing and implementing computer programs* is called **programming**.\n",
    "![scanner.jpg](attachment:scanner.jpg)\n",
    "- The physical computer and peripheral devices are collectively called the **hardware**. \n",
    "- The programs the computer executes are called the **software**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The Anatomy of a Computer\n",
    "The Computer **Hardware** consists of the physical elements in a computer system.  \n",
    "- The **central processing unit (CPU)** performs program control and data processing\n",
    "- Storage devices include \n",
    "    - **Primary memory**: Consists of memory chips (electronic circuits that can store data as long as it is provided electric power).\n",
    "        - Fast and more expensive.\n",
    "        - e.g., RAM and ROM\n",
    "    - **Secondary storage**: Provides slower, less expensive storage that is persistent (without electric power)\n",
    "        - e.g., Hard disks, flash drives, CD/DVD drives.\n",
    "    - Computers store both data and programs\n",
    "        - Both are located in secondary storage and are loaded into primary storage when programs are executed.\n",
    "- **Input/output** devices allow the user to interact with the computer \n",
    "    - Mouse, keyboard, printer, screen"
   ]
  },
  {
   "attachments": {
    "CPU.jpg": {
     "image/jpeg": "[encoded data, 90104 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## The Central Processing Unit (CPU)\n",
    "- The CPU has two components, the **control unit** and the **arithmetic logic unit** ![CPU.jpg](attachment:CPU.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- The **control unit** directs operation of the processor. \n",
    "    - Computer resources are managed by the control unit.\n",
    "    - Controls communication and co-ordination between input/output devices. \n",
    "    - Reads and interprets instructions and determines the sequence for processing the data.\n",
    "    - Provides timing and control signals "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- The **arithmetic logic unit** contains the circuitry to perform calculations and do comparisons. \n",
    "    - It is the workhorse portion of the computer and its job is to do precisely what the control unit tells it to do."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": []
  },
  {
   "attachments": {
    "ComputerComponents.jpg": {
     "image/jpeg": "[encoded data, 68208 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "![ComputerComponents.jpg](attachment:ComputerComponents.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- Program instructions and data (such as text, numbers, audio, or video) are stored on the hard disk, on a compact disk (or DVD), or elsewhere on the network. \n",
    "- When a program starts, it is brought into memory, where the CPU can read it. \n",
    "    - one instruction at a time. \n",
    "    - The CPU reads, modifies and writes data back to memory or the hard disk. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "- Some program instructions will cause the CPU to place dots on the display screen or printer or to vibrate the speaker. \n",
    "- Some program instructions read user input from the keyboard or mouse. \n",
    "    - The program analyzes the nature of these inputs and then executes the next appropriate instruction."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The Python Programming Language\n",
    "- High-level programming languages specify a large number of simple CPU instructions with much fewer understandable statements.\n",
    "    - Specifying CPU instructions can be tedious and error-prone."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- One very popular high level language is Python.\n",
    "    - Developed by Guido van Rossum in the early 1990s.\n",
    "    - Wanted a language suitable for writing smaller programs that may not run at optimum speed.\n",
    "        - As opposed to other languages like Java or C."
   ]
  },
  {
   "attachments": {
    "vanRossum.JPG": {
     "image/jpeg": "[encoded data, 55340 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "![vanRossum.JPG](attachment:vanRossum.JPG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some reasons for Python's succsess:\n",
    "    - Simpler and cleaner syntax than Java, C, and C++. \n",
    "    - Can try out short Python programs in an interactive environment.\n",
    "    - Portable between computer systems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Becoming Familiar with Your Programming Environment\n",
    "- There are several ways of creating a computer program\n",
    "    - Using an Integrated Development Environment (IDE)\n",
    "    - Using a text editor \n",
    "- We will use the **Jupyter Notebook** (Details on installing it will be provided in the first lab)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- **Jupyter Notebook** is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.\n",
    "- Slides of this course have been developed using Jupyter.\n",
    "- A big advantage is that we use the same platform to **present** and **run** Python code.\n",
    "- Let us look at our first Python program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# My first Python program.\n",
    "print(\"Hello, World!\")\n",
    "print(\"Hello\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## How do Python programs run?\n",
    "1. The compiler reads your source code (that is, the Python instructions that you wrote) all at once.\n",
    "1. The compiler translates the instructions into **byte code**.\n",
    "    - **Byte codes** are very simple instructions understood by the **virtual machine** (a separate program that is similar to the CPU of a computer). \n",
    "1. Any necessary libraries are automatically located and included by the virtual machine.\n",
    "    - For example, the implementation of the **`print`** function.\n",
    "1. The virtual machine executes your byte code."
   ]
  },
  {
   "attachments": {
    "PythonInterpreter.jpg": {
     "image/jpeg": "[encoded data, 29116 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "   ![PythonInterpreter.jpg](attachment:PythonInterpreter.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Analyzing Your First Program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "# My first Python Program\n",
    "print(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The first line is a comment.\n",
    "- Comments start with a # and are not considered statements (ignored by the interpreter)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The second line displays a line of text, viz., **`Hello, World!`** using the **`print`** function.\n",
    "- A **function** is a *collection* of programming instructions that carry out a particular task.\n",
    "- **`\"Hello, World!\"`** is called a **string**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- To use, or call, a function in Python, you need to specify:\n",
    "- The name of the function you want to use (in this case, **`print`**).\n",
    "- Any values the function needs to carry out its task (in this case, **`\"Hello, World!\"`**).\n",
    "    - The technical term for such a value is an argument. \n",
    "    - Arguments are enclosed in parentheses.\n",
    "    - Multiple arguments are separated by commas. \n",
    "    - The number of arguments required depends on the function."
   ]
  },
  {
   "attachments": {
    "printStatement.JPG": {
     "image/jpeg": "[encoded data, 80720 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- Syntax of the **`print`** statement\n",
    "![printStatement.JPG](attachment:printStatement.JPG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## More Examples of the Print Statement\n",
    "- Printing numerical values "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n"
     ]
    }
   ],
   "source": [
    "print(3 + 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Passing multiple values to the function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The answers of adding and multiplying are 13 42 , respectively\n"
     ]
    }
   ],
   "source": [
    "print(\"The answers of adding\" , \"and multiplying are\", 6 + 7, 6 * 7, \", respectively\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Note that each value passed to the function is displayed, one after another, with a blank space after each value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- By default the print function starts a new line after its arguments are printed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "print(\"Hello\")\n",
    "print(\"World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Our Second Program (printtest.py)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "Hello\n",
      "World!\n",
      "My favorite numbers are 7 and 13\n",
      "Goodbye\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-b5c73aca26e7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;31m# Prints three lines of text with a blank line.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Goodbye\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     18\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Hope to see you again\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "##\n",
    "#  Sample program that demonstrates the print function.\n",
    "#\n",
    "\n",
    "# Prints 7.\n",
    "print(3 + 4) \n",
    "      \n",
    "# Prints \"Hello World!\" in two lines.\n",
    "print(\"Hello\") \n",
    "print(\"World!\")\n",
    "\n",
    "# Prints multiple values with a single print function call.\n",
    "print(\"My favorite numbers are\", 3 + 4, \"and\", 3 + 10) \n",
    "\n",
    "# Prints three lines of text with a blank line.\n",
    "print(\"Goodbye\")   \n",
    "print(1/0)\n",
    "print(\"Hope to see you again\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Errors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- There are two Categories of Errors:\n",
    "    - Compile-time Errors (Syntax Errors) \n",
    "    - Run-time Errors (Logical Errors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Compile-time Errors\n",
    "- Spelling, capitalization, punctuation\n",
    "- Ordering of statements, matching of parenthesis, quotes, ...etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "EOL while scanning string literal (<ipython-input-4-d85c16c463f2>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-d85c16c463f2>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    print(\"Hello, World!)\u001b[0m\n\u001b[1;37m                         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n"
     ]
    }
   ],
   "source": [
    "## Uncomment each statement.\n",
    "print(\"Hello, World!)\n",
    "#Print(\"Hello, World!\")\n",
    "#print(\"Hello World!')\n",
    "#print('Hello'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- No executable program is created by the compiler\n",
    "- Correct first error listed, then compile again. \n",
    "- Repeat until all errors are fixed"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Run-time Errors\n",
    "- The program runs, but produces unintended results\n",
    "- The program may **crash**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "## Uncomment each statement.\n",
    "#print(\"Hello, Word!\")\n",
    "#print(1/0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Note that run-time errors are more troublesome. They are the harder to find and fix because the interpreter cannot flag them for us. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Problem Solving (Algorithm Design)\n",
    "- Self Reading\n",
    "- The topic will be discussed in Lab 2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Summary\n",
    "## Computer Basics\n",
    "- Computers rapidly execute very simple instructions\n",
    "- A Program is a sequence of instructions and decisions\n",
    "- Programming is the art (and science) of designing, implementing, and testing computer programs\n",
    "- The Central  Processing Unit (CPU) performs program control and data processing\n",
    "- Storage devices include memory and secondary storage (e.g., a USB Flash Drive)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Python\n",
    "- Python was designed in a way that makes it easier to learn than other programming languages such as Java, C and C++.\n",
    "- The designers goal was to give Python simpler and cleaner syntax.\n",
    "- Set aside some time to become familiar with the programming environment that you will use for your class work.\n",
    "    - It is important to practice with the tool so you can focus on learning Python\n",
    "- An editor is a program for entering and modifying text, such as a Python program.\n",
    "- Python is case sensitive. \n",
    "    - You must be careful about distinguishing between upper and lowercase letters.\n",
    "- The Python compiler translates source code into byte code instructions that are executed by the virtual machine.\n",
    "- A function is called by specifying the function’s name and its parameters.\n",
    "- A string is a sequence of characters enclosed in quotation marks."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Errors\n",
    "- A compile-time error is a violation of the programming language rules that is detected by the compiler.\n",
    "- A run-time error causes a program to take an action that the programmer did not intend."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
